package com.yusp75.myexam.ui.setting

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import androidx.fragment.app.Fragment
import com.yusp75.myexam.databinding.FragmentSettingBinding
import org.koin.androidx.viewmodel.ext.android.viewModel

class SettingFragment : Fragment() {

    private var _binding: FragmentSettingBinding? = null
    private val binding get() = _binding!!
    private val viewModel: SettingViewModel by viewModel()

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View {


        _binding = FragmentSettingBinding.inflate(inflater, container, false)
        val root: View = binding.root

        val choiceView: EditText = binding.numOfChoice
        viewModel.numOfChoice.observe(viewLifecycleOwner) { value ->
            value?.let { num ->
                // 在主线程更新UI
                choiceView.post {
                    choiceView.setText(num.toString()) // 用setText()而非直接赋值
                }
            }
        }

        viewModel.choiceHint.observe(viewLifecycleOwner) { value ->
            choiceView.setHint(value)
        }
        // 验证 1
        choiceView.addRangeValidator(max = viewModel.getNumOfChoice()) { viewModel.saveNumChoice(it) }

        val judgeView: EditText = binding.numOfJudge
        viewModel.numOfJudge.observe(viewLifecycleOwner) { value ->
            value?.let { num ->
                judgeView.post {
                    judgeView.setText(num.toString())
                }
            }
        }
        viewModel.judgeHint.observe(viewLifecycleOwner) { value ->
            judgeView.setHint(value)
        }
        // 验证2
        judgeView.addRangeValidator(max = viewModel.getNumOfJudge()) { viewModel.saveNumJudge(it) }
        //
        return root
    }


    private fun EditText.addRangeValidator(
        min: Int = 1,
        max: Int,
        onValid: (Int) -> Unit
    ) {
        var previousValue = text.toString()

        addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
                previousValue = s?.toString() ?: ""
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) = Unit

            override fun afterTextChanged(s: Editable?) {
                s?.toString()?.trim()?.toIntOrNull()?.let { newValue ->
                    when {
                        newValue < min || newValue > max -> {
                            setText(previousValue)
                            setSelection(previousValue.length)
                        }

                        else -> onValid(newValue)
                    }
                }
            }
        })
    }


    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    //
}